<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>类别管理</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
        }

        .container {
            width: 60%;
            margin: 0 auto;
        }

        h1 {
            text-align: center;
        }

        .messages {
            list-style-type: none;
            padding: 0;
            margin: 0;
            text-align: center;
        }

        .message {
            background-color: #f1f1f1;
            padding: 10px;
            margin-bottom: 10px;
        }

        form {
            margin-bottom: 20px;
            display: flex;
            align-items: center;
            flex-wrap: wrap;
        }

        label {
            margin-right: 10px;
            flex-basis: 100%;
        }

        input[type="text"],
        select {
            padding: 10px;
            font-size: 14px;
            border-radius: 4px;
            border: 1px solid #ccc;
            flex-grow: 1;
            margin-bottom: 10px;
            width: 100%;
        }

        button {
            padding: 10px;
            font-size: 14px;
            border-radius: 4px;
            background-color: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
            width: 100%;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            border: 1px solid #ddd;
            background: linear-gradient(to bottom, #f9f9f9, #ececec);
        }

        th, td {
            padding: 10px;
            text-align: center;
            border: 1px solid #ddd;
        }

        th {
            font-weight: bold;
        }

        .editbtn, .deletebtn {
            padding: 5px 10px;
            font-size: 16px;
            border-radius: 4px;
            background-color: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
            width: 80px;
        }

        th:nth-child(5),
        td:nth-child(5),
        th:nth-child(6),
        td:nth-child(6) {
            width: 80px; /* 设置编辑和删除列宽度 */
        }

        th:nth-child(1),
        th:nth-child(2),
        th:nth-child(3),
        th:nth-child(4),
        td:nth-child(1),
        td:nth-child(2),
        td:nth-child(3),
        td:nth-child(4) {
            width: auto; /* 其他列自适应宽度 */
        }

        .editbtn {
            background-color: #008CBA;
        }

        .deletebtn {
            background-color: #f44336;
        }

        @media only screen and (max-width: 600px) {
            /* 对于小于等于 600px 宽度的屏幕 */
            table {
                font-size: 12px; /* 调整表格字体大小 */
            }

            th, td {
                padding: 5px; /* 调整表格单元格内边距 */
            }

            .editbtn, .deletebtn {
                font-size: 14px; /* 调整编辑和删除按钮字体大小 */
                width: 60px; /* 调整编辑和删除按钮宽度 */
            }

            th:nth-child(5),
            td:nth-child(5),
            th:nth-child(6),
            td:nth-child(6) {
                width: 60px; /* 调整编辑和删除列宽度 */
            }

            th:nth-child(1),
            th:nth-child(2),
            th:nth-child(3),
            th:nth-child(4),
            td:nth-child(1),
            td:nth-child(2),
            td:nth-child(3),
            td:nth-child(4) {
                width: auto; /* 其他列自适应宽度 */
            }
        }
    </style>
</head>
<body>
<div class="container">
    <h1>产品类别管理</h1>
    {% with messages = get_flashed_messages() %}
    {% if messages %}
    <ul class="messages">
        {% for message in messages %}
        <li class="message">{{ message }}</li>
        {% endfor %}
    </ul>
    {% endif %}
    {% endwith %}
    <form id="category-form" action="/submit_class" method="POST">
        <label for="class_name">类别名称：</label>
        <input type="text" id="class_name" name="class_name" required>
        <label for="class_code">类别编号：</label>
        <input type="text" id="class_code" name="class_code">
        <label for="parent_directory">上一级目录：</label>
        <select id="parent_directory" name="parent_directory">
            <option value="">请选择上级目录</option>
            {% for row in class_list %}
            <option value="{{row[0]}}">{{row[1]}}</option>
            {% endfor %}
        </select>
        <button type="submit">添加类别</button>
    </form>


    <table id="category-table">
        <thead>
        <tr>
            <th>类别ID</th>
            <th>名称</th>
            <th>编号</th>
            <th>上一级目录</th>
            <th>编辑</th>
            <th>删除</th>
        </tr>
        </thead>
        <tbody>
        {% for row in class_list %}
        <tr>
            <td id="id" name="id">{{ row[0] }}</td>
            <td>{{ row[1] }}</td>
            <td>{{ row[2] }}</td>
            <td>{{ row[3] }}</td>
            <td>
                <form action="/class_edit/{{ row[0] }}" method="GET">
                    <button type="submit" class="editbtn">编辑</button>
                </form>
            </td>
            <td>
                {% if row[0] != 1 %}
                <form action="/delete/{{ row[0] }}" method="POST">
                    <button type="submit" class="deletebtn">删除</button>
                </form>
                {% else %}
                <!-- 显示提示信息，说明为什么不能删除 -->
                <span>默认类别无法删除</span>
                {% endif %}
            </td>
        </tr>
        {% endfor %}
        </tbody>
    </table>
</div>

<script>
</script>
</body>
</html>
